﻿Imports System.IO

Public Class BigTypeList

    Private _是否更新 As Boolean = False
    Public Property 是否更新() As Boolean
        Get
            Return _是否更新
        End Get
        Set(ByVal value As Boolean)
            _是否更新 = value
        End Set
    End Property

    Private Sub InitForm()
        是否更新 = True

        Dim dt As DataTable = Util.GetSoftWareBigType
        Me.dgType.DataSource = dt
        For index As Integer = 1 To Me.dgType.ColumnCount - 1
            Me.dgType.Columns(index).ReadOnly = False
        Next

        RefreshForm()
    End Sub

    Private Sub TypeList_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        InitForm()

        是否更新 = False

    End Sub

    Private Sub RefreshForm()

        Dim totalWidth As Integer = dgType.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) + dgType.RowHeadersWidth
        If dgType.DisplayedRowCount(False) <> dgType.RowCount Then
            Me.Width = totalWidth + SystemInformation.BorderSize.Width * 2 + SystemInformation.VerticalScrollBarWidth + dgType.Margin.Left + dgType.Margin.Right
        Else
            Me.Width = totalWidth + SystemInformation.BorderSize.Width * 2 + dgType.Margin.Left + dgType.Margin.Right
        End If

    End Sub


    Private Sub 添加ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 添加ToolStripMenuItem.Click

        Dim maxId As String = "0001"
        If Me.dgType.RowCount > 0 Then
            maxId = CType(dgType.DataSource, DataTable).Compute("max(id)", "")
            maxId = CStr(CInt(maxId) + 1).PadLeft(4, "0"(0))
        End If

        Dim ty As New CSoftWareBigType
        ty.Id = maxId
        Using frm As New BigTypeAdd(ty)
            frm.Text = "添加软件大分类"
            AddHandler frm.Updateparent, AddressOf InitForm
            If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
                'Dim 软件类型 As CSoftWareBigType = frm.软件类型
                'Dim dr As DataRow = CType(Me.dgType.DataSource, DataTable).NewRow
                'dr("id") = 软件类型.Id
                'dr("name") = 软件类型.Name
                'dr("image") = Image.FromFile(Util.GetCurrentFileIcon(软件类型.IconPath, CInt(软件类型.IconpathType))).GetThumbnailImage(32, 32, Nothing, IntPtr.Zero)
                'dr("imagepath") = 软件类型.IconPath
                'CType(Me.dgType.DataSource, DataTable).Rows.Add(dr)

            End If
        End Using

    End Sub

    Private Sub 修改ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 修改ToolStripMenuItem.Click

        If Me.dgType.RowCount = 0 Then
            Return
        End If

        If Me.dgType.SelectedRows.Count = 0 Then
            Return
        End If

        Dim ty As New CSoftWareBigType
        ty.Id = Me.dgType.SelectedRows(0).Cells("colNao").Value.ToString
        ty.Name = Me.dgType.SelectedRows(0).Cells("colName").Value.ToString
        ty.IconPath = Me.dgType.SelectedRows(0).Cells("colPath").Value.ToString
        Using frm As New BigTypeAdd(ty)
            frm.Text = "修改软件大分类"
            If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
                是否更新 = True
                Dim 软件类型 As CSoftWareBigType = frm.软件类型
                Dim dr As DataRow = CType(Me.dgType.DataSource, DataTable).Rows(Me.dgType.SelectedRows(0).Index)
                dr("id") = 软件类型.Id
                dr("name") = 软件类型.Name
                dr("image") = Image.FromFile(Util.GetCurrentFileIcon(软件类型.IconPath, CInt(软件类型.IconpathType))).GetThumbnailImage(32, 32, Nothing, IntPtr.Zero)
                dr("imagepath") = 软件类型.IconPath
                RefreshForm()
            End If
        End Using

    End Sub

    Private Sub 删除ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 删除ToolStripMenuItem.Click

        If Me.dgType.RowCount = 0 Then
            Return
        End If
        If Me.dgType.SelectedRows.Count = 0 Then
            Return
        End If

        If wys.util.Common.MessageBase.ShowMessage("是否删除此条记录？", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
            是否更新 = True
            Dim cby As New XMLSoftWareBigType
            Dim cy As New XMLSoftWareType
            cby.Delete(Me.dgType.SelectedRows(0).Cells("colNao").Value.ToString)
            cy.DeleteBigType(Me.dgType.SelectedRows(0).Cells("colNao").Value.ToString)
            Me.dgType.Rows.RemoveAt(Me.dgType.SelectedRows(0).Index)
            RefreshForm()
        End If

    End Sub

    Private Sub dgType_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgType.DoubleClick
        修改ToolStripMenuItem_Click(Nothing, Nothing)
    End Sub

    Private Sub dgType_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgType.CellContentClick
        If e.RowIndex >= 0 And e.ColumnIndex = Me.dgType.Columns("colType").Index Then
            Dim ty As New XMLSoftWare
            Dim cws As List(Of CSoftWare) = ty.GetByTypeId(Me.dgType.SelectedRows(0).Cells("colNao").Value.ToString)
            If cws.Count = 0 Then
                Return
            End If
            Using frm As New TypeList(Me.dgType.SelectedRows(0).Cells("colNao").Value.ToString)
                frm.ShowDialog()
            End Using
        End If
    End Sub
End Class
